package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.conmon.MyPageData;
import com.woniuxy.conmon.enums.RedisConst;
import com.woniuxy.conmon.enums.SystemCode;
import com.woniuxy.pojo.NewsArticle;
import com.woniuxy.pojo.dto.NewsArticleDto;
import com.woniuxy.service.NewsArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsArticleController {
    @Autowired
    private NewsArticleService newsArticleService;
    @Autowired
    RedisTemplate redisTemplate;
    @PostMapping("/getNewsByType/{typeId}/{pageNum}/{pageSize}")
    public ResponseEntity<?> getNewsByType(@PathVariable Integer typeId,
                                           @PathVariable Integer pageNum,
                                           @PathVariable Integer pageSize,
                                           @RequestBody(required = false) NewsArticleDto dto){
        Page<NewsArticle> page = newsArticleService.getNewsByType(typeId,pageNum,pageSize,dto);
        MyPageData data = new MyPageData(page.getTotal(), page.getRecords());
        return ResponseEntity.ok(data);
    }
    @GetMapping("/getAuthors")
    public ResponseEntity<?> getAuthors(){
        List<NewsArticle> authors = newsArticleService.getAuthors();
        return ResponseEntity.ok(authors);
    }
    @DeleteMapping("/deleteNewsById/{id}")
    public ResponseEntity<?> deleteNewsById(@PathVariable Integer id){
        boolean flag = newsArticleService.removeById(id);
        return ResponseEntity.ok(flag? SystemCode.SUCCESS.getMessage():SystemCode.FAIL.getMessage());
    }
    @PutMapping("/offShelve/{id}")
    public ResponseEntity<?> offShelve(@PathVariable Integer id){
        LambdaUpdateWrapper<NewsArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(NewsArticle::getId,id).set(NewsArticle::getStatus,5);
        boolean flag = newsArticleService.update(wrapper);
        return ResponseEntity.ok(flag? SystemCode.OFF_SHELVE_SUCCESS.getMessage():SystemCode.FAIL.getMessage());
    }
    @PutMapping("/sticky/{id}")
    public ResponseEntity<?> sticky(@PathVariable Integer id){
        LambdaUpdateWrapper<NewsArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(NewsArticle::getId,id).set(NewsArticle::getTopLevel,1);
        boolean flag = newsArticleService.update(wrapper);
        return ResponseEntity.ok(flag? SystemCode.STICKY_SUCCESS.getMessage():SystemCode.FAIL.getMessage());
    }
    @PutMapping("/cancelSticky/{id}")
    public ResponseEntity<?> cancelSticky(@PathVariable Integer id){
        LambdaUpdateWrapper<NewsArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(NewsArticle::getId,id).set(NewsArticle::getTopLevel,0);
        boolean flag = newsArticleService.update(wrapper);
        return ResponseEntity.ok(flag? SystemCode.STICKY_SUCCESS.getMessage():SystemCode.FAIL.getMessage());
    }
    @PutMapping("/submit/{id}")
    public ResponseEntity<?> submit(@PathVariable Integer id){
        LambdaUpdateWrapper<NewsArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(NewsArticle::getId,id).set(NewsArticle::getStatus,2);
        boolean flag = newsArticleService.update(wrapper);
        return ResponseEntity.ok(flag? SystemCode.SUCCESS.getMessage():SystemCode.FAIL.getMessage());
    }
    @PutMapping("/withdraw/{id}")
    public ResponseEntity<?> withdraw(@PathVariable Integer id){
        LambdaUpdateWrapper<NewsArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(NewsArticle::getId,id).set(NewsArticle::getStatus,1);
        boolean flag = newsArticleService.update(wrapper);
        return ResponseEntity.ok(flag? SystemCode.WITHDRAW_SUCCESS.getMessage():SystemCode.FAIL.getMessage());
    }
    @GetMapping("/getNewsById/{id}")
    public ResponseEntity<?> getNewsById(@PathVariable Integer id){
        NewsArticle news = newsArticleService.getById(id);
        return ResponseEntity.ok(news);
    }
    @PostMapping("/saveOrEditNews")
    public ResponseEntity<?> saveOrEditNews(@RequestBody NewsArticle news){
        news.setTypeId(1);
        news.setCommentCount(100);
        news.setReadCount(100);
        news.setShareCount(100);
        Boolean flag=newsArticleService.saveOrUpdate(news);
        if (flag){
            redisTemplate.opsForSet().add(RedisConst.UPLOAD_IMAGES_DB,news.getThumbnail());
        }
        return ResponseEntity.ok(SystemCode.SUCCESS.getMessage());
    }
}
